package net.sevear.chaos
{
	import com.douban.auth.DBAuthenticate;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	import net.sevear.chaos.download.DBRadioKeeper;
	import net.sevear.chaos.download.Predef;
	import net.sevear.chaos.event.ChaosTrackEvent;
	import net.sevear.chaos.utils.DataUtil;
	import net.sevear.chaos.vo.TrackInfo;

	public class ChaosTrackManager extends EventDispatcher
	{
		private static const TRACK_INFO_URL:String = "/t";
		
		private var isSendingRequest:Boolean = false;
		private var chaosloader:URLLoader;
		
		public static const TYPE_INFO:int = 0;
		public static const TYPE_INSTANT:int = 1;
		
		private var _type:int = TYPE_INFO;
		public function ChaosTrackManager()
		{
			super(null);
			chaosloader = new URLLoader;
			confirgureURLLoader(onGetTrackInfo,chaosloader);
		}
		
		private static var instance:ChaosTrackManager;
		public static function getInstance():ChaosTrackManager{
			if(instance == null){
				instance = new ChaosTrackManager();
			}
			return instance;
		}
		
		public function replay(sid:int):void{
			getTrackInfo(sid);
		}
		
		public function replay2(trackInfo:TrackInfo):void{
			if(trackInfo.ssid!="" && trackInfo.ssid!=null){
				var instantTrack:TrackInfo = new TrackInfo();
				DataUtil.generateNewTrack(instantTrack,trackInfo);
				instantTrack.subtype = TrackInfo.INSTANT;
				instantTrack.addEventListener(TrackInfo.UPDATE_DOUBAN_URL,onGetDBListenInfo);
				instantTrack.addEventListener(TrackInfo.FAILED_UPDATE_DOUBAN_URL,onFailedGetDBURL);
				instantTrack.getDBRadioURL();
			}
		}
		
		public function getTrackInfo(sid:int,type:int = TYPE_INSTANT):void{
			if(isSendingRequest==false){
				_type = type;
				isSendingRequest = true;
				var rst:URLRequest = new URLRequest(encodeURI(Predef.CHAOS_HOST+TRACK_INFO_URL));
				var vars:URLVariables = new URLVariables();
				vars.sid = sid;
				vars.uid = DBAuthenticate.getInstance().dbUID;
				rst.data = vars;
				chaosloader.load(rst);
			}
		}
		
		private function confirgureURLLoader(completeFunc:Function,loader:URLLoader):void{
			loader.addEventListener(Event.COMPLETE,completeFunc);
			loader.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecurityError);
		}
		
		private function onGetTrackInfo(e:Event):void{
			isSendingRequest = false;
			var trackInfo:TrackInfo = DataUtil.parseRemoteTrack((e.currentTarget as URLLoader).data as String);
			if(trackInfo!=null){
				if(_type==TYPE_INSTANT){
					trackInfo.subtype = TrackInfo.INSTANT;
					trackInfo.addEventListener(TrackInfo.UPDATE_DOUBAN_URL,onGetDBListenInfo);
					trackInfo.addEventListener(TrackInfo.FAILED_UPDATE_DOUBAN_URL,onFailedGetDBURL);
					trackInfo.getDBRadioURL();
				}else{
					dispatchEvent(new ChaosTrackEvent(ChaosTrackEvent.CHAOS_TRACK_INFO_UPDATED,trackInfo));
				}
			}
		}
		
		private function onGetDBListenInfo(e:Event):void{
			var track:TrackInfo = e.currentTarget as TrackInfo;
			track.listenURL = track.downURLs[DBRadioKeeper.SOURCE_DB_RADIO] as String;
			track.removeEventListener(TrackInfo.UPDATE_DOUBAN_URL,onGetDBListenInfo);
			track.removeEventListener(TrackInfo.FAILED_UPDATE_DOUBAN_URL,onFailedGetDBURL);
			dispatchEvent(new ChaosTrackEvent(ChaosTrackEvent.NEW_CHAOS_REPLAY_ARRIVED,track));
		}
		
		private function onFailedGetDBURL(e:Event):void{
			
		}
		
		private function onIOError(e:IOErrorEvent):void{
			
		}
		
		private function onSecurityError(e:SecurityErrorEvent):void{
			
		}
		
	}
}